<template>
  <MyAlert 
    type="success" 
    message="一级必须是菜单，二级可以是链接或者组，组下边必须是链接"
  />
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增链接</a-button>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleShowSelectLink"> 预览选择链接</a-button>
      <a-button type="primary" preIcon="ic:round-expand" @click="expandAll">展开全部</a-button>
      <a-button type="primary" preIcon="ic:round-compress" @click="collapseAll">折叠全部</a-button>

      <a-dropdown v-if="checkedKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button>批量操作
          <Icon icon="ant-design:down-outlined" />
        </a-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>
  </BasicTable>
  <InputForm @register="registerDrawer" @success="handleSuccess" :showFooter="showFooter" />
  <SceneListDrawer @register="registerSceneListDrawer"/>
  <SelectLink @register="registerModal"/>
</template>
<script lang="ts" name="biz-app-page-link" setup>
import { nextTick, ref, onMounted } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { useDrawer } from '/@/components/Drawer';
import { SelectLink } from '/@/components/BizAppSelectLink/index';
import InputForm from './Form.vue';
import SceneListDrawer from '/@/views/system/scene/SceneListDrawer.vue';
import { columns, searchFormSchema } from './data';
import { list, deleteMenu, batchDeleteMenu } from './api';
import { useRouter } from "vue-router"
import { useMessage } from '/@/hooks/web/useMessage';
import { Icon } from '/@/components/Icon';
import { useModal } from '/@/components/Modal';
import MyAlert from '/@/components/My/MyAlert.vue';

const [registerModal, { openModal }] = useModal();
const { createMessage } = useMessage();
const router = useRouter()

const checkedKeys = ref<Array<string | number>>([]);
const showFooter = ref(true);
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerSceneListDrawer, { openDrawer: openSceneListDrawer }] = useDrawer();

const { prefixCls, tableContext } = useListPage({
  tableProps: {
    title: '页面链接',
    api: list,
    immediate: true,
    formConfig: {
      schemas: searchFormSchema,
    },
    columns: columns,
    size: 'small',
    pagination: false,
    isTreeTable: true,
    striped: true,
    useSearchForm: true,
    showTableSetting: true,
    bordered: false,
    showIndexColumn: false,
    tableSetting: { fullScreen: true },
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  }
})

const [registerTable, { reload, getForm, expandAll, collapseAll }] = tableContext

const rowSelection = {
  type: 'checkbox',
  columnWidth: 30,
  selectedRowKeys: checkedKeys,
  onChange: onSelectChange,
};

onMounted(() => {
 
})

const handleShowSelectLink = () => {
  openModal(true, {})
}

const handleOpenScene = () => {
  openSceneListDrawer(true, {
    isUpdate: true
  });
}

function onSelectChange(selectedRowKeys: (string | number)[]) {
  checkedKeys.value = selectedRowKeys;
}

function handleCreate() {
  openDrawer(true, { isUpdate: false });
}

function handleEdit(record) {
  showFooter.value = true;
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleDetail(record) {
  showFooter.value = false;
  openDrawer(true, {
    record,
    isUpdate: true
  });
}

function handleAddSub(record) {
  
  openDrawer(true, {
    record: { 'parentId': record.id, 'menuType': "1", parentMenuType: record.type },
    isUpdate: false,
  });
}


async function handleDelete(record) {
  await deleteMenu({ id: record.id }, reload);
}

async function batchHandleDelete() {
  await batchDeleteMenu({ ids: checkedKeys.value }, reload);
}

function handleSuccess() {
  reload();
}

function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: `是否确认删除 ${record.name}`,
        confirm: handleDelete.bind(null, record),
      },
    },
  ];
}

function getDropDownAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '添加下级',
      onClick: handleAddSub.bind(null, record),
    },

  ];
}
</script>
